package com.iwhalecloud.bss.kite.cucc.common.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;




/**
 * <p>Title: GX INTERFACE </p>
 * <p>Description: 网厅等接口XML报文组装组件,将LinkedHashMap数据组装成XML</p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: </p>
 * @author Arthur xu
 * @version 1.0
 */

public class XmlLinkedEncapInstance  {
    private LinkedHashMap rootName;
    private XMLLinkedTools tools;

    public XmlLinkedEncapInstance() {
        tools = new XMLLinkedTools();
    }

    public void setRootName(LinkedHashMap rootName) {
        this.rootName = rootName;
    }
    
    public Object encapsulation(Object request) throws Exception {
    	LinkedHashMap rst = (LinkedHashMap) request;
        String xml = null;
        
        Iterator keyIt = rst.keySet().iterator();
        while (keyIt.hasNext()) {
        	String rootKey = (String) keyIt.next();
        	
        	XMLLinkedObject root = new XMLLinkedObject(rootKey);
        	
        	XMLLinkedObject content = encapCDATA(rootKey, (LinkedHashMap) rst.get(rootKey), root, rootKey);
        	tools.execute(content);
            xml = tools.getDocString();
        }
        return xml;
    }

    private XMLLinkedObject encapCDATA(String rootName, LinkedHashMap contMap, XMLLinkedObject content, String rootKey) throws
    Exception {
        if (contMap != null) {
            if (contMap.get(rootKey) != null) {
            	content = new XMLLinkedObject((String) contMap.get(rootKey));
                contMap.remove(rootKey);
            } 
            else {
            	content = new XMLLinkedObject(rootKey);
            }

            Set set = contMap.keySet();
            Iterator it = set.iterator();

            while (it.hasNext()) {
                String name = (String) it.next();
                if (contMap.get(name) instanceof String) {
                    encapString(name, (String) contMap.get(name), content);
                }
                if (contMap.get(name) instanceof LinkedHashMap) {
                    encapHashMap(name, (LinkedHashMap) contMap.get(name), content);
                }
                if (contMap.get(name) instanceof ArrayList) {
                    encapArrayList(name, (ArrayList) contMap.get(name), content);
                }
                if (contMap.get(name) == null) {
                    encapString(name, "", content);
                }
            }
        }
        else {
            content.addProd(rootName, "");
        }
        
        return content;
    }

	private XMLLinkedObject encapCDATA(String rootName, LinkedHashMap contMap, XMLLinkedObject content) throws
            Exception {
        if (contMap != null) {
            if (contMap.get("ContractRoot") != null) {
            	content = new XMLLinkedObject((String) contMap.get("ContractRoot"));
                contMap.remove("ContractRoot");
            } 
            else {
            	content = new XMLLinkedObject("ContractRoot");
            }

            Set set = contMap.keySet();
            Iterator it = set.iterator();

            while (it.hasNext()) {
                String name = (String) it.next();
                if (contMap.get(name) instanceof String) {
                    encapString(name, (String) contMap.get(name), content);
                }
                if (contMap.get(name) instanceof LinkedHashMap) {
                    encapHashMap(name, (LinkedHashMap) contMap.get(name), content);
                }
                if (contMap.get(name) instanceof ArrayList) {
                    encapArrayList(name, (ArrayList) contMap.get(name), content);
                }
                if (contMap.get(name) == null) {
                    encapString(name, "", content);
                }
            }
        }
        else {
            content.addProd(rootName, "");
        }
        
        return content;
    }

    private void encapString(String rootName, String value, XMLLinkedObject content) throws
            Exception {
        content.addProd(rootName, value);
    }

    private void encapHashMap(String rootName, LinkedHashMap contMap,
    					XMLLinkedObject content) throws Exception {
        content.addProd(rootName, contMap);
    }

    private void encapArrayList(String rootName, ArrayList contList, XMLLinkedObject content) throws Exception {
        Iterator it = contList.iterator();
        while (it.hasNext()) {
            Object value = it.next();
            if (value instanceof String) {
                content.addProd(rootName, (String) value);
            }
            if (value instanceof LinkedHashMap) {
                content.addProd(rootName, (LinkedHashMap) value);
            }
            if (value == null) {
                content.addProd(rootName, "");
            }
        }
    }

    

    public Object encapsulation(Object request, Object label) throws Exception {
    	LinkedHashMap rst = (LinkedHashMap) request;
        String rn = (String) label;
        String xml = null;
        XMLLinkedObject root = new XMLLinkedObject(rn);

        Set set = rootName.keySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            String name = (String) it.next();
            if ("String".equals(rootName.get(name))) {
                encapString(name, rst.get(name).toString(), root);
            }
            if ("LinkedHashMap".equals(rootName.get(name))) {
                encapHashMap(name, (LinkedHashMap) rst.get(name), root);
            }
            if ("CDATA".equals((String) rootName.get(name))) {
                encapCDATA(name, (LinkedHashMap) rst.get(name), root);
            }
        }

        tools.execute(root);
        xml = tools.getDocString();

        return xml;
    }
}
